package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.BaseDTO;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.workorder.pojo.dto.WorkOrderTemplateListAllDTO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderGetWaitRobVO;
import com.xbongbong.pro.workorder.pojo.vo.WorkOrderTemplateListAllVO;
import com.xbongbong.saas.help.SaasListHelp;
import com.xbongbong.saas.service.WorkOrderSearchService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 * @author 刘阳
 * @date 2019/4/3 5:08 PM
 * @since v1.0
 * @version v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/workOrder/search")
public class WorkOrderSearchController {
    @Resource
    private WorkOrderSearchService workOrderSearchService;
    @Resource
    private SaasListHelp saasListHelp;

    /**
     * 全部工单模板（没有分类）
     *
     * @param workOrderTemplateListAllDTO 入参
     * @param br BindingResult
     * @return java.lang.String
     * @author 徐俊杰
     * @date 2019/4/24 14:14
     * @since v1.0
     */
    @RequestMapping(value = "/getTemplateListAll", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getTemplateListAll(@RequestBody @Valid WorkOrderTemplateListAllDTO workOrderTemplateListAllDTO, BindingResult br) {
        XbbResponse<WorkOrderTemplateListAllVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderTemplateListAllVO workOrderTemplateListAllVO;
                try {
                    workOrderTemplateListAllVO = workOrderSearchService.getTemplateListAll(workOrderTemplateListAllDTO);
                } catch (IllegalAccessException | InstantiationException e) {
                    throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
                }
                response = new XbbResponse<>(workOrderTemplateListAllVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
    /**
     * Description: 获取待抢单数
     * @param baseDTO
     * @param br
     * @return java.lang.String
     * @author 魏荣杰
     * @date 2019/5/13 19:44
     * @since v1.0
     */
    @RequestMapping(value = "/getWaitRob", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getWaitRob(@RequestBody @Valid BaseDTO baseDTO, BindingResult br) {
        XbbResponse<WorkOrderGetWaitRobVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                WorkOrderGetWaitRobVO  workOrderGetWaitRobVO = new WorkOrderGetWaitRobVO();
                workOrderGetWaitRobVO.setTotal(saasListHelp.getWaitRob(baseDTO.getLoginUser()));
                response = new XbbResponse<>(workOrderGetWaitRobVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response);
    }
}
